package com.jiang.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiang.demo.dao.mapper.User2Mapper;
import com.jiang.demo.entity.User2Entity;
import com.jiang.demo.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class User2Service {

    @Autowired
    private User2Mapper user2Mapper;


    public Long save(User2Entity entity){
        user2Mapper.insert(entity);
        return entity.getId();
    }

    public Long delete(Long id){
        User2Entity dbEntity = user2Mapper.selectById(id);
        dbEntity.setIsDelete(true);
        user2Mapper.insert(dbEntity);
        return dbEntity.getId();
    }

    public Long update(UserEntity entity){
        User2Entity dbEntity = user2Mapper.selectById(entity.getId());
        BeanUtils.copyProperties(entity, dbEntity);
        user2Mapper.updateById(dbEntity);
        return dbEntity.getId();
    }

    public List<User2Entity> getUserList(){
        return user2Mapper.selectList(new LambdaQueryWrapper<>());
    }

    public IPage<User2Entity> getUserPage(int currentPage, int pageSize){
        Page<User2Entity> page = new Page<>(currentPage, pageSize);
        return user2Mapper.selectPage(page, Wrappers.emptyWrapper());
    }

}
